package com.it.schoolbookshop_back.controller;

import com.it.schoolbookshop_back.entities.po.Result;
import com.it.schoolbookshop_back.entities.po.User;
import com.it.schoolbookshop_back.service.UserService;
import com.it.schoolbookshop_back.utils.UserContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户资料控制器
 */
@RestController
@RequestMapping("/api/profile")
public class ProfileController {

    @Autowired
    private UserService userService;

    /**
     * 获取当前登录用户的详细信息
     * 需要token验证
     * @return 用户详细信息
     */
    @GetMapping
    public Result<Map<String, Object>> getMyProfile() {
        // 从ThreadLocal中获取当前登录用户ID
        Integer userId = UserContext.getUserId();
        if (userId == null) {
            return Result.failed(401, "未登录或token已过期");
        }
        
        // 查询用户信息
        User user = userService.getUserById(userId);
        if (user == null) {
            return Result.failed("用户不存在");
        }
        
        // 构建返回数据
        Map<String, Object> profileData = new HashMap<>();
        profileData.put("username", user.getUsername());
        profileData.put("real_name", user.getReal_name());
        profileData.put("department", user.getDepartment());
        profileData.put("student_id", user.getStudent_id());
        profileData.put("email", user.getEmail());
        profileData.put("phone", user.getPhone());
        profileData.put("wechat", user.getWechat());
        profileData.put("bio", user.getBio());
        profileData.put("bookCount", user.getBookCount());
        profileData.put("soldCount", user.getSoldCount());
        profileData.put("boughtCount", user.getBoughtCount());
        profileData.put("favoriteCount", user.getFavoriteCount());
        profileData.put("avatar", user.getAvatar_type() != null ? true : false);
        
        return Result.success(profileData);
    }
    
    /**
     * 修改用户资料
     * 需要token验证
     * @param profileData 要修改的资料
     * @return 修改结果
     */
    @PutMapping
    public Result<Map<String, Object>> updateProfile(@RequestBody Map<String, Object> profileData) {
        // 从ThreadLocal中获取当前登录用户ID
        Integer userId = UserContext.getUserId();
        if (userId == null) {
            return Result.failed(401, "未登录或token已过期");
        }
        
        // 查询用户信息
        User user = userService.getUserById(userId);
        if (user == null) {
            return Result.failed("用户不存在");
        }
        
        // 更新可以修改的字段
        if (profileData.containsKey("real_name")) {
            user.setReal_name((String) profileData.get("real_name"));
        }
        if (profileData.containsKey("department")) {
            user.setDepartment((String) profileData.get("department"));
        }
        if (profileData.containsKey("student_id")) {
            user.setStudent_id((String) profileData.get("student_id"));
        }
        if (profileData.containsKey("email")) {
            user.setEmail((String) profileData.get("email"));
        }
        if (profileData.containsKey("phone")) {
            user.setPhone((String) profileData.get("phone"));
        }
        if (profileData.containsKey("wechat")) {
            user.setWechat((String) profileData.get("wechat"));
        }
        if (profileData.containsKey("bio")) {
            user.setBio((String) profileData.get("bio"));
        }
        
        // 调用服务层保存修改
        boolean success = userService.updateUser(user);
        if (!success) {
            return Result.failed("更新个人资料失败");
        }
        
        // 构建返回数据
        Map<String, Object> updatedProfile = new HashMap<>();
        updatedProfile.put("username", user.getUsername());
        updatedProfile.put("real_name", user.getReal_name());
        updatedProfile.put("department", user.getDepartment());
        updatedProfile.put("student_id", user.getStudent_id());
        updatedProfile.put("email", user.getEmail());
        updatedProfile.put("phone", user.getPhone());
        updatedProfile.put("wechat", user.getWechat());
        updatedProfile.put("bio", user.getBio());
        
        return Result.success("个人资料更新成功", updatedProfile);
    }
} 